using UnityEngine;
using System.Collections;

public class PickingManager : MonoBehaviour {

    private enum TypePicking { HUD, PERSO, TILE };

    public Camera cam;
    public bool mouseLeftBtn;
    public bool mouseLeftBtnClick;
    public bool mouseRightBtn;

    public Vector2 mouse;
    public Vector2 mouseMove = new Vector2(0, 0);

    public Element selectedElement;
    public Element selectedHUDElement;
    public Element selectedTile;


    // Use this for initialization
    public void Start()
    {
        mouseLeftBtn = false;
        mouseRightBtn = false;
        mouseLeftBtnClick = false;

        mouse = Input.mousePosition;

    }

    // Update is called once per frame
    void Update()
    {

        mouse = Input.mousePosition;

        //Mouse Left Button
        if (Input.GetMouseButtonDown(0) && !mouseLeftBtn)
        {
            mouseLeftBtn = true;
            mouseLeftBtnClick = true;

        }

        if (Input.GetMouseButtonUp(0))
        {
            mouseLeftBtn = false;
            ClickRelease();
        }

        //Picking
        Picking();

        //Mouse Right Button
        if (Input.GetMouseButtonDown(1))
            mouseRightBtn = true;

        if (Input.GetMouseButtonUp(1))
            mouseRightBtn = false;

        mouse = Input.mousePosition;

        //Drag
        if (mouseLeftBtn && selectedElement != null)
        {
            selectedElement.DragOn();
        }

        mouseLeftBtnClick = false;
    }

    //Click
    void Picking()
    {

        //HUD picking
        if (!PickingCamera(cam, Const.LAYER_HUD, selectedHUDElement, TypePicking.HUD))
        {


        }

    }


    bool PickingCamera(Camera cam, int layer, Element elem, TypePicking type)
    {
        //GUI Picking
        Ray ray = cam.ScreenPointToRay(new Vector3(mouse.x, mouse.y, 0));
        RaycastHit hit;

        //Raycast
        if (Physics.Raycast(ray, out hit, 1000, 1 << layer))
        {
            //Return for drag perso action
            if (!mouseLeftBtnClick && mouseLeftBtn && type == TypePicking.PERSO && elem != null)
                return true;

            //Select Element
            if (hit.transform.GetComponent<Element>())
            {

                //Change Selected Object
                if (hit.transform.GetComponent<Element>() != elem && elem != null)
                {
                    //Mouse Quit
                    elem.MouseQuit();
                    elem = null;
                }

                elem = hit.transform.GetComponent<Element>();


                //Click On
                if (mouseLeftBtnClick)
                {
                    elem.ClickOn();
                }
                //Mouse Over
                else if (!mouseLeftBtn)
                {
                    elem.MouseOver();
                }

            }
            else
            {
                if (elem != null)
                {
                    //Mouse Quit
                    elem.MouseQuit();

                }
                elem = null;
            }

            ReaffectValue(type, elem);

            return true;
        }
        else
        {
            //Mouse Quit
            if (elem != null)
            {
                elem.MouseQuit();

                if (type == TypePicking.HUD)
                    elem = null;
                else if (type == TypePicking.PERSO)
                {
                    //if (mouseLeftBtnClick)
                    //    elem = null;
                }
                else if (type == TypePicking.TILE)
                {
                    elem = null;
                }
            }
        }

        ReaffectValue(type, elem);
        return false;

    }


    void ReaffectValue(TypePicking type, Element elem)
    {
        if (type == TypePicking.PERSO)
        {
            selectedElement = elem;
        }
        else if (type == TypePicking.HUD)
        {
            selectedHUDElement = elem;
        }
        else if (type == TypePicking.TILE)
        {
            selectedTile = elem;
        }
    }

    //Click Release
    void ClickRelease()
    {
        if (selectedElement != null)
        {
            selectedElement.ClickRelease();
            selectedElement = null;
        }

        if (selectedHUDElement != null)
        {
            selectedHUDElement.ClickRelease();
            selectedHUDElement = null;
        }
        if (selectedTile != null)
        {
            selectedTile.ClickRelease();
            selectedTile = null;
        }
    }
}
